const app = getApp();
var config = app.globalData.config;
const http = require('../../utils/http.js');

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		leaveDay: '',
		curriculumList: [],
		coursearrangeList:[],
		addData: {
			curriculum_index: "",
			curriculum_id: "",
			coursearrange_index:"",
			coursearrange_id:""
		}, //补卡要提交的数据
	},
	onLoad(options) {
		this.getTeacherCurriculumList();
	},
	getTeacherCurriculumList() { //获取教师管理课程
		var that = this;
		http.httpPost(config.getTeacherCurriculumList, {
		}, function(e) {
			if (e.code == 1) {
				// console.log(e);
				that.setData({
					'curriculumList': e.data
				});
			} else {
		
			}
		
		});
	},
	getTeacherCoursearrangeList() { //获取课节
		var that = this;
		http.httpPost(config.getTeacherCoursearrangeList, {
			curriculum_id:that.data.addData.curriculum_id,
			dtime:that.data.leaveDay
		}, function(e) {
			if (e.code == 1) {
				// console.log(e);
				that.setData({
					'coursearrangeList': e.data
				});
			} else {
		
			}
		
		});
	},
	//请假开始时间
	leaveDayChange: function(e) {
		let addData = this.data.addData;
		addData.leavetime = e.detail.value;
		this.setData({
			leaveDay: e.detail.value,
			addData: addData
		});
	},
	//请假课程
	curriculumChange: function(e) {
		let addData = this.data.addData;
		addData.curriculum_index = e.detail.value; //选中的课程列表下标
		addData.curriculum_id = this.data.curriculumList[e.detail.value].id;
		this.setData({
			addData: addData
		});
		
		//获取课节
		this.getTeacherCoursearrangeList();
	},
	//选择课节
	coursearrangeChange: function(e) {
		let addData = this.data.addData;
		addData.coursearrange_index = e.detail.value; //选中的课程列表下标
		addData.coursearrange_id = this.data.coursearrangeList[e.detail.value].id;
		
		this.setData({
			addData: addData
		});
		console.log(this.data.addData);
	},
	addStudentLeave() { //提交btn
		var _leavereason = this.data.leavereason;
		var _curriculum_id = this.data.addData.curriculum_id;
		var _coursearrange_id = this.data.addData.coursearrange_id;
    var _leavetime = this.data.addData.leavetime;
    
		if (_leavereason == "" || _leavereason == undefined) {
			wx.showToast({
				title: '请输入【补卡事由】',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
		}

		if (_curriculum_id == "" || _curriculum_id == undefined) {
			wx.showToast({
				title: '请选择【补卡课程】',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
		}
		if(_coursearrange_id=="" || _coursearrange_id==undefined){
			wx.showToast({
				title: '请选择【补卡课节】',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
		}
		if (_leavetime == "" || _leavetime == undefined) {
			wx.showToast({
				title: '请选择【补卡时间】',
				icon: 'none',
				duration: 2000,
				mask: true
			});
			return false;
		}

		var that = this;
		http.httpPost(config.addCardData, {
			curriculum_id: _curriculum_id,
			coursearrange_id:_coursearrange_id,
			leavereason: _leavereason,
			leavetime: _leavetime
		}, function(e) {
			var _code = e.code;
			var _msg = e.msg;
			var _icon = "none";
			var _duration = 2000;
			if (_code == 1) {
				_icon = "success";
			} else {
				_icon = "error";
				_duration = 3500;
			}

			wx.showToast({
				title: _msg,
				icon: _icon,
				duration: _duration,
				mask: true
			})

			if (_code == 1) {
				setTimeout(function() {
					wx.redirectTo({
						url: '../Supplementary_card/Supplementary_card'
					})
				}, 2000);
			}

		});

	},
	getLeavereason:function(e){	//获取请假事由值
		let _leavereason = e.detail.value;
		this.setData({
			leavereason: _leavereason
		});
  },
  checkboxChange(e) {
    // console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    // console.log(e)
    const coursearrangeList = this.data.coursearrangeList
    const values = e.detail.value
    let addData = this.data.addData;
    addData.coursearrange_id = values.toString();;
 
    for (let i = 0; i < coursearrangeList.length; i++) {
      coursearrangeList[i].checked = false

      for (let j = 0; j < values.length; j++) {
        if (coursearrangeList[i].id == values[j]) {
          coursearrangeList[i].checked = true
          break
        }
      }
    }
    this.setData({
      coursearrangeList:coursearrangeList,
      addData:addData
    })
  },
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {
		if (!app.login()) {
			return;
		}
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})
